<template>
  <div class="login-container">
    <div class="container">
      <div class="loginLeft">
        <img src="../assets/login.png"/>
      </div>
      <div class="loginRight">
        <div class="title1">你好！ 欢迎访问</div>
        <div class="title2">访客管理系统</div>
        <el-form :model="form">
          <el-form-item label="账号" size="default">
            <el-input @keyup.enter="handleLogin" v-model="form.username" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item label="密码" size="default">
            <el-input
                @keyup.enter="handleLogin" v-model="form.password" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-button class="login" @click="handleLogin" :loading="loading">登录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import {useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import {reactive, ref, onMounted, inject} from 'vue'

import {getToken, setRefreshToken, setRole, setToken} from '@/utils/auth'
import login from "./login.vue";
import {loginApi} from "@/api/login";


const form = reactive({
  // account: '15298707245',
  username: '',
  password: ''
})
const router = useRouter()
const loading = ref(false)


const handleLogin = () => {
  if (!form.username) {
    ElMessage.warning("请输入账号！")
    return false;
  } else if (!form.password) {
    ElMessage.warning("请输入密码！")
    return false;
  } else {
    loading.value = true;
    ElMessage.closeAll();
    loginApi(form).then((response: any) => {
      console.log(response)
      if (response.code == 200) {
        loading.value = false
        setToken(response.data.id)
        setRole(response.data.role)
        ElMessage.success(response.msg)
        router.push({path: '/main'})
      } else {
        ElMessage.warning(response.msg)
      }
    }).catch(res => {
      loading.value = false
    }).finally(() => {
      loading.value = false;
    })
  }
}

</script>

<style lang="scss" scoped>

.login-container {
  height: 100%;
  width: 100%;
  background-image: url('../assets/background.png');
  background-size: 100% 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.container {
  width: 1100px;
  height: 550px;
  background-image: url('../assets/dialog.png');
  background-size: 100% 100%;
  margin-top: 80px;
  margin-left: auto;
  margin-right: auto;
}

.loginLeft {
  float: left;
  width: 420px;
  height: 100%;
  margin-left: 150px;

  img {
    width: 350px;
    height: 300px;
    margin-top: 150px;
    margin-left: 30px;
  }
}

.loginRight {
  float: left;
  width: 450px;
  height: 100%;
  padding-left: 20px;
}

.title1 {
  line-height: 50px;
  color: #1e2676;
  font-size: 34px;
  margin-top: 80px;
  letter-spacing: 6px;
}

.title2 {
  margin-top: 5px;
  font-weight: bold;
  font-size: 36px;
  letter-spacing: 4px;
  line-height: 50px;
  color: #1e2676;
}

.el-form {
  margin-top: 60px;
}

:deep(.el-form-item__content) {
  font-size: 18px;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-input__wrapper) {
  padding: 0px !important;
}

:deep(.el-input__inner) {
  font-size: 18px;
  border: 0px;
  line-height: 50px;
  padding: 5px 0px;
  // border-bottom: solid 1px #ddd;
  width: 280px;
  color: #333;
  border-radius: 0;
  box-shadow: 0 0px 0px #ccc;
  margin-left: 10px;
  //   &::placeholder {
  //     color:#333;
  //   }
}

.login {
  width: 320px;
  height: 40px;
  border-radius: 20px;
  font-size: 16px;
  color: #fff;
  background-color: #3c81f6;
  margin-top: 40px;
  margin-left: 15px;
}

</style>
